package com.huisang.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huisang.domain.Customer;
import com.huisang.mapper.CustomerMapper;
import com.huisang.service.CustomerService;
import com.huisang.util.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageInfo<Customer> findCustomerAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Customer> customers = customerMapper.finCustomerAll();
        PageInfo<Customer> pageInfo = new PageInfo<Customer>(customers);
        return pageInfo;
    }

    @Override
    public ResponseEntity<Integer> addCustomer(Customer customer) {
        ResponseEntity<Integer> responseEntity = new ResponseEntity<>();
        int i = customerMapper.addCustomer(customer);
        if(i > 0) {
            responseEntity.setCode(200);
            responseEntity.setMessage("添加数据成功！");
            responseEntity.setData(i);
            return responseEntity;
        }
        responseEntity.setCode(500);
        responseEntity.setMessage("添加数据失败");
        responseEntity.setData(i);
        return responseEntity;
    }

    @Override
    public ResponseEntity<Integer> deleteCustomer(Integer id) {
        ResponseEntity<Integer> responseEntity = new ResponseEntity<>();
        int i = customerMapper.deleteCustomer(id);
        if(i > 0) {
            responseEntity.setCode(200);
            responseEntity.setMessage("删除数据成功！");
            responseEntity.setData(i);
            return responseEntity;
        }
        responseEntity.setCode(500);
        responseEntity.setMessage("删除数据失败");
        responseEntity.setData(i);
        return responseEntity;
    }

    @Override
    public ResponseEntity<Integer> updateCustomer(Customer customer) {
        ResponseEntity<Integer> responseEntity = new ResponseEntity<>();
        int i = customerMapper.updateCustomer(customer);
        if(i > 0) {
            responseEntity.setCode(200);
            responseEntity.setMessage("修改数据成功！");
            responseEntity.setData(i);
            return responseEntity;
        }
        responseEntity.setCode(500);
        responseEntity.setMessage("修改数据失败");
        responseEntity.setData(i);
        return responseEntity;
    }

    @Override
    public ResponseEntity<Customer> findOneCustomerById(Integer id) {
        ResponseEntity<Customer> responseEntity = new ResponseEntity<>();
        Customer customer = customerMapper.findOneCustomerById(id);
        if(customer != null) {
            responseEntity.setCode(200);
            responseEntity.setMessage("查找数据成功！");
            responseEntity.setData(customer);
            return responseEntity;
        }
        responseEntity.setCode(500);
        responseEntity.setMessage("查找数据失败");
        responseEntity.setData(null);
        return responseEntity;
    }

    @Override
    public List<Customer> findAllCustomer2() {
        List<Customer> allCustomer = customerMapper.finCustomerAll();
        return allCustomer;
    }
}
